<?php
/**
 * User: Mark
 * Time: 13-6-24 下午2:10
 */
require '../../common.inc.php';
// 获取一条老的数据
	// passenger
	$sql = "select * from xd_auto_rv where isisis=0 order by id desc limit 1 ";
	// name
	$modelOld = $db->getOne($sql);
    if (!$modelOld) {exit(date("Y-m-d H:i:s"));}
	$sql = "select * from xd_auto where aid={$modelOld['aid']}";
	$carOld = $db->getOne($sql);
// 插入新的车款主表

		// year [2012款] => 239
		$year = handleYear($modelOld['market_time']);

		// 之前的车系ID
		$oldCarname = $carOld['name'];
			// 检查车系名称是否和现有的匹配, 如果匹配, 现在的ID就已经可以获取到
			// 现在有重复值正在检测中
		// 现在车系ID匹配中

		// aid  [车系id]
			// arbid
			// armid
			// arcid

		$ids = getMatchedids($oldCarname);
		$arbid = $ids[0];
		$armid = $ids[1];
		$arcid = $ids[2];

		// match acid 关联汽车的级别
		// acid     => 乘用/商用/房车   , 在之前分类中匹配现在的分级
		$acid = getAcid($carOld['cid']);
		// price 之前的价格 * 10000
		$price = formatPrice($modelOld['price']);

		// fuel_num  => enginetype
		$enginetype = getEngineType($modelOld['fuel_type']);
		//             emission
		$emission = getEmission($modelOld['fuel_type']);
		// transmission_type   =>  gearbox
		$gearbox = getGearbox($modelOld['gear_box_type']);
		// status     =>      issale[在售/停售]
		$issale = 1;
		// auto_model
		// modeltype        =>  'rv'
		$insert = "insert into xd_auto_model(
		name, year, arbid, armid, arcid, acid, price, enginetype, emission, gearbox, issale, modeltype,country, listorder
		) values (
		'{$modelOld['name']}',
		'{$year}',
		'{$arbid}',
		'{$armid}',
		'{$arcid}',
		'{$acid}',
		'{$price}',
		'{$enginetype}',
		'{$emission}',
		'{$gearbox}',
		'{$issale}',
		'rv',
		0,
		20
		)";
		$db->query($insert);
		$amid = $db->insertId();

	// 插入新的车款附表
		// auto_model_rv
	$newFields = PCache::read('fields-auto_model_rv.php');
	$name = array();
	$name['amid'] = $amid;

	foreach($newFields as $k => $field) {
		if (isset($modelOld[$field['name']])) {
			$name[$field['name']] = $modelOld[$field['name']];
		}
	}


	$insertMf = Sql::insert('xd_auto_model_rv', $name);
	var_dump($modelOld['id']);
	var_dump($modelOld['name']);
//	print_r($insertMf);exit();
	var_dump($db->query($insertMf));
			// 对应参数
	$sql = "update xd_auto_rv set isisis=1 where id={$modelOld['id']}";
	var_dump($db->query($sql));
echo '<meta http-equiv="refresh" content="0; url='.$_SERVER['PHP_SELF'].'" />';
function handleYear($old) {
	$years = PCache::read('autoCategory-year.php');
	foreach($years as $k => $v) {
		if ($v['name'] == $old) {
			return $k;
		}
	}
	return '0';
}

function getMatchedids($name) {
	global $db;
	$sql = "select arid, arrparentid from xd_auto_category where name='{$name}'";
	$ifo = $db->getOne($sql);
	if ($ifo) {
		$pids = explode(',', $ifo['arrparentid']);
		array_shift($pids);
		$pids[] = $ifo['arid'];
		return $pids;
	} else {
		return array(
			4274,4275,4276
		);
	}
}

function getAcid($oldAcid) {
	$oldLevel = PCache::read('oldAutoBrand.php');
	$oldLevelName = $oldLevel[$oldAcid]['catname'];
	$newLevel = PCache::read('autoCategory-level.php');
	foreach($newLevel as $k => $v) {
		if ($oldLevelName == $v['name']) {
			return $k;
		}
	}
	return 0;
}

function formatPrice($oldPrice) {
	return round($oldPrice*10000,0);
}

function getEngineType($tp) {
	//1=>"93#汽油",2=>"97号汽油",3=>"0号柴油",4=>"天然气",5=>"98#汽油"
	// 426 -> 汽油
	// 427 -> 柴油
	// 428 -> 天然气
	$arr = array(
		'柴油/国3' => 427,
		'汽油/欧4' => 426,
		'柴油/国IV' => 427,
		'柴油/欧IV' => 427,
		'汽油/国Ⅲ' => 426,
		'汽油/国4' => 426,
		'柴油/国4' => 427,
		'柴油/ 国' => 427,
		'柴油/国' => 427,
		'柴油/欧4' => 427,
		'柴油/国4+OBD' => 427,
		'汽油/国3' => 426,
		'汽油/欧Ⅳ' => 426,
		'汽油/欧IV' => 426,
		'/国4' => 426,
	);
	if (isset($arr[$tp])) {
		return $arr[$tp];
	}
	return 0;
}

function getGearbox($tp) {
	$arr = array(
		'0' => 4314,
	);
	if (isset($arr[$tp])) {
		return $arr[$tp];
	}
	return 4314;
}

function getEmission($tp) {
	$ge = array('欧IV+OBD','国IV/国V','国Ⅰ','欧Ⅱ','欧II','国II','国Ⅱ','欧II/欧V','欧洲2号','国 Ⅱ','欧Ⅱ 国II', '欧I','欧Ⅰ或欧Ⅱ','达低速货车、三轮车第二阶段排放要求','欧Ⅰ','欧Ι');
	if(in_array($tp, $ge)) {
		return 4326;
	}
	$gs = array(
		'柴油/国3',
		'汽油/国Ⅲ',
		'国Ⅲ/国IV',
		'国Ⅲ/',
		'国 Ⅲ','国三',
		'国Ⅲ/国II',
		'国Ⅱ、国Ⅲ',
		'国IV/欧IV',
		'国III/欧III',
		'国Ⅲ、国IV',
		'汽油/国3',
	);
	if(in_array($tp, $gs)) {
		return 519;
	}
	$gsi = array('汽油/欧4','柴油/欧IV','汽油/国4','柴油/国4','柴油/ 国','柴油/欧4','欧IV，可选欧III','欧Ⅳ','国IVI',
		'柴油/国4+OBD',
		'汽油/欧Ⅳ',
		'/国4',
	);
	if(in_array($tp, $gsi)) {
		return 520;
	}
	$gw = array('柴油/国IV', '欧V','欧VI','欧V（SCR）','国V','直列',
		'欧五',
		'汽油/欧IV',

	);
	if(in_array($tp, $gw)) {
		return 521;
	}

	return 0;
}